﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using RentalDTO;
using RentalIService;
using RentalService.Entities;
using Common;

namespace RentalService
{
    public class ProcessItemService : IServiceProcessItem
    {
      

        public int AddProcessItem(string Id, string Name, string Code, int IsDel)
        {
            using (var db = new RentalContext())
            {
                BaseService<ProcessItem> bs = new BaseService<ProcessItem>(db);

                ProcessItem item = new ProcessItem();
                item.Id = Id;
                item.Code = Code;
                item.Name = Name;
                item.IsDel = IsDel;

                if( bs.Add(item))
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        }

        public int DelProcessItem(string id)
        {
            using (var db = new RentalContext())
            {
                BaseService<ProcessItem> bs = new BaseService<ProcessItem>(db);
                var model = bs.GetLists(u => u.Id == id).FirstOrDefault();
                model.IsDel = 0;

                if (bs.Delete(model))
                {

                    return 1;
                }
                else
                {
                    return 0;
                }
            }
    }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="limit">每页显示的数据</param>
        /// <param name="offset">从第几页开始</param>
        /// <param name="Name">流程名称</param>
        /// <param name="Code">流程代码</param>
        /// <param name="count">返回总数</param>
        /// <param name="sort">排序字段</param>
        /// <param name="order">排序顺序</param>
        /// <returns></returns>
        public List<ProcessItemDTO> ListPage(int limit, int offset, string Name, string Code, ref int count, string sort, string order)
        {
            using (var db = new RentalContext())
            {
                BaseService<ProcessItem> bs = new BaseService<ProcessItem>(db);

                Expression<Func<ProcessItem, bool>> whereLambda = u => u.IsDel == 1;
                if (!string.IsNullOrWhiteSpace(Name))
                {
                    whereLambda = whereLambda.And(u => u.Name.Contains(Name));
                }
                if (!string.IsNullOrWhiteSpace(Code))
                {
                    whereLambda = whereLambda.And(u => u.Code.Contains(Code));
                }
                var orderBy = PredicateExtensions.GetSortLambda<ProcessItem>(sort);
                bool orderType = order == "asc" ? true : false;
                var list = bs.GetPagedList(offset, limit, ref count, whereLambda, orderBy, orderType).Select(u => new ProcessItemDTO
                {
                    Name = u.Name,
                    Code = u.Code,
                    Id = u.Id
                }).ToList();
                return list;
            }
        }
    }
}
